package com.example.demotest.controller;

import java.io.*;

public class readAndWriteTest {

    public static void main(String[] args) {
        //读取路径
        String readPath = "C:\\Users\\chenjie\\IdeaProjects1\\demo-test\\src\\main\\resources\\wdad.txt";
        //写入路径
        String writePath ="C:\\Users\\chenjie\\IdeaProjects1\\demo-test\\src\\main\\resources\\write.txt";
        File file = new File(readPath);
        try{
            //构造一个BufferedReader类来读取文件
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
            //构造一个BufferedWriter类来写入文件
            FileWriter vFileWriter = new FileWriter(writePath);
            BufferedWriter vBufferedWriter = new BufferedWriter(vFileWriter);
            String s = null;
            //正则替换
            while((s = br.readLine())!=null){//使用readLine方法，一次读一行
                //先逐行写入
                String s1 = s.replaceAll("Acct-Status-Type =", " ").replaceAll("Acct-Session-Id = ","")
                        .replaceAll("User-Name =","").replaceAll("Acct-Session-Id =","")
                        .replaceAll("Framed-IP-Address =","")
                        .replaceAll("Framed-IPv6-Prefix =","")
                        .replaceAll("Calling-Station-Id =","")
                        .replaceAll("NAS-IP-Address =","")
                        .replaceAll("Event-Timestamp =","")
                        .replaceAll("Tmp-String-9 =","")
                        .replaceAll("Acct-Unique-Session-Id =","")
                        .replaceAll("Timestamp =","");
                vBufferedWriter.write(s1);
//                System.out.println(s1 +s1.length());
                if(s.length()==0){
                    vBufferedWriter.newLine();
                }

            }
            br.close();
            vBufferedWriter.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}